/**
 * <p>Package : org.tea.springdata.mina</p>
 * <p>E-Mail : ztlyde@gmail.com</p>
 */
package org.tea.springdata.mina.config;

import java.nio.charset.Charset;

import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.DatagramSessionConfig;
import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
 * @author: John zhang
 * @version: 0.1
 */
@Configuration
@ComponentScan(basePackages = { "org.tea.springdata.mina" })
public class MinaConfig {
	
	@Bean
	public LoggingFilter loggingFilter() {
		return new LoggingFilter();
	}
	
	@Bean
	public ProtocolCodecFilter textLineCodecFilter() {
		ProtocolCodecFilter textLienFilter = new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName("UTF-8"), LineDelimiter.NUL, LineDelimiter.NUL ) );
		return textLienFilter;
	}
	
	@Bean
	public ProtocolCodecFilter objectCodecFilter() {
		ProtocolCodecFilter objectFilter = new ProtocolCodecFilter( new ObjectSerializationCodecFactory() );
		return objectFilter;
	}
	
	@Bean
	public ExecutorFilter executorFilter() {
		ExecutorFilter filter = new ExecutorFilter( 10 );
		return filter;
	}
	
	/**
	 * 应用于(TCP/IP)
	 * @return
	 */
	@Bean
	@DependsOn( { "loggingFilter", "textLineCodecFilter", "objectCodecFilter" } )
	@Autowired( required = true )
	public NioSocketAcceptor nioSocketAcceptor(LoggingFilter loggingFilter, ProtocolCodecFilter textLineCodecFilter, ProtocolCodecFilter objectCodecFilter) {
		NioSocketAcceptor socketAcceptor = new NioSocketAcceptor();
		socketAcceptor.getFilterChain().addLast( "logging", loggingFilter );
		// socketAcceptor.getFilterChain().addLast( "objectCodec", objectCodecFilter );
		socketAcceptor.getFilterChain().addLast( "textCodec", textLineCodecFilter );
		return socketAcceptor;
	}
	
	/**
	 * 应用与(UDP/IP)
	 * @return
	 */
	@Bean
	@DependsOn( { "loggingFilter", "executorFilter" } )
	@Autowired( required = true )
	public NioDatagramAcceptor nioDatagramAcceptor(ExecutorFilter executorFilter) {
		NioDatagramAcceptor datagramAcceptor = new NioDatagramAcceptor();
		datagramAcceptor.getFilterChain().addLast( "executorFilter", executorFilter );
		
		DatagramSessionConfig config = datagramAcceptor.getSessionConfig();
		config.setReadBufferSize( 4096 ); //设置接收最大字节默认4096
		config.setReceiveBufferSize( 1024 ); //设置输入缓冲区的大小
        config.setSendBufferSize( 1024 ); //设置输出缓冲区的大小
        config.setReuseAddress( true ); //设置每一个非主监听连接的端口可以重用
		return datagramAcceptor;
	}
	
}
